package com.ego.dubbo.service.impl;


import com.ego.commons.pojo.EasyUITree;
import com.ego.dubbo.service.TBItemCatDubboService;
import com.ego.mapper.TbItemCatMapper;
import com.ego.pojo.TbItemCat;
import com.ego.pojo.TbItemCatExample;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class TBItemCatDubboServiceImpl implements TBItemCatDubboService {


    @Autowired
    private TbItemCatMapper tbItemCatMapper;

    @Override
    public List<EasyUITree> selectItemCatByParentId(long parentId) {

        TbItemCatExample tbItemCatExample = new TbItemCatExample();
        tbItemCatExample.createCriteria().andParentIdEqualTo(parentId);
        //商品分类数据
        List<TbItemCat> tbItemCats = tbItemCatMapper.selectByExample(tbItemCatExample);

        //需要将商品分类数据转换成EasyUITree需要的数据
        List<EasyUITree> easyUITrees = new ArrayList<>();
        for (TbItemCat tbItemCat : tbItemCats) {
            //创建EasyUITree对象
            EasyUITree easyUITree = new EasyUITree();
            easyUITree.setId(tbItemCat.getId());
            easyUITree.setText(tbItemCat.getName());
            easyUITree.setState(tbItemCat.getIsParent()?"closed":"open");
            easyUITrees.add(easyUITree);
        }

        return easyUITrees;
    }

    @Override
    public TbItemCat selectById(long id) {
        return tbItemCatMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<TbItemCat> selectItemCatsByParentId(long parentId) {

        TbItemCatExample tbItemCatExample = new TbItemCatExample();
        tbItemCatExample.createCriteria().andParentIdEqualTo(parentId);
        //商品分类数据
        List<TbItemCat> tbItemCats = tbItemCatMapper.selectByExample(tbItemCatExample);
        return tbItemCats;
    }
}
